<template>
  <div>
    <el-form ref="registerForm" :model="registerform" :rules="rules" label-width="80px" >
      <h3 class="register-title">注册用户</h3>
      <el-form-item label="账号" prop="user">
        <el-input type="text" placeholder="请输入账号" v-model="registerform.user"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" placeholder="请输入密码" v-model="registerform.password"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="logining" v-on:click="onSubmit()">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Axios from 'axios';
export default{
  data() {
    return {
      logining:false,
      registerform: {
        user: '',
        password: ''
      },
      // 表单验证，需要在 el-form-item 元素中增加 prop 属性
      rules: {
        user: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不可为空', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    onSubmit() {
      // console.log(this.form)
      // 为表单绑定验证功能
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.logining=true
          let _this = this
          if (_this.logining == true) {
            Axios.get("http://localhost:8181/admin/register",{params:this.registerform}).then(function (res) {
              _this.logining=false
              // console.log(res.data.code)
              if (res.data.code == -1) {
                _this.$message.error('用户名已存在');
              }
              if (res.data.code == 0) {
                _this.$message({
                  message: '恭喜你，注册成功',
                  type: 'success'
                });
                localStorage.setItem('login',JSON.stringify(res.data.data));
                _this.$router.push({path:'/'})
              }
            })
          }

        }
        // 使用 vue-router 路由到指定页面，该方式称之为编程式导航
        // this.$router.push("/main");
        // } else {

        //   // return false;
        // }
      });
    }
  }
}

</script>

<style>
.register-title {
  text-align: center;
  margin: 0 auto 40px auto;
  color: #303133;
}
</style>